
import sys
import asyncio

from charfinder import UnicodeNameIndex

CRLF = b'\r\n'
PROMPT = b'?> '

index = UnicodeNameIndex()

@asyncio.coroutine
def handle_queries(reader,writer):
    while True:
        writer.write(PROMPT)
        try:
            yield from writer.drain()
        except ConnectionResetError:
            break
        data = yield from reader.readline()
        try:
            query = data.decode().strip()
        except UnicodeDecodeError:
            query = '\x00'
        client = writer.get_extra_info('peername')
        print("Received from {}:{!r}".format(client,query))
        if query:
            if ord(query[:1]) < 32: #编码值小于32
                break
            lines = list(index.find_description_strs(query))
            if lines:
                writer.writelines(line.encode()+CRLF for line in lines)
            writer.write(index.status(query,len(lines)).encode()+CRLF)
            yield from writer.drain()
            print('Sent {} result'.format(len(lines)))
        print('Close the client socket')
        writer.close()

def main(address='localhost',port=2323):
    port = int(port)
    loop = asyncio.get_event_loop()
    print(address,port)
    server_coro = asyncio.start_server(handle_queries,address,port,loop=loop) 
    server = loop.run_until_complete(server_coro)
    host = server.sockets[0].getsockname()
    print('Serving on {}.hit CTRL-C to stop '.format(host))
    try:
        loop.run_forever()   
    except KeyboardInterrupt:
        pass
    print("Server shutdown ")
    server.close()
    loop.run_until_complete(server.wait_closed())
    loop.close()

if __name__ == '__main__':
    main(*sys.argv[1:])

